/*
 *
 * Saturn Framework Co.Limt
 * Copyright (c) 2012-2019 tanks, Inc. All rights reserved.
 */

package com.saturn.config.server.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.saturn.config.server.base.R;
import com.saturn.config.server.base.entity.ConfigLog;
import com.saturn.config.server.service.ConfigLogService;

import lombok.AllArgsConstructor;

/**
 * <dl>
 * <dt><b>Title:</b></dt>
 * <dd>none</dd>
 * <dt><b>Description:</b></dt>
 * <dd>
 * <p>
 * 配置操作日志表</dd>
 *
 * @author tanks
 * @date 2019-07-08 14:50:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/configlog")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ConfigLogController {

	private final ConfigLogService configLogService;

	/**
	 * 分页查询
	 * @param page      分页对象
	 * @param configLog 配置操作日志表
	 * @return
	 */
	@RequestMapping(value = "/page", produces = "application/json")
	public @ResponseBody IPage<?> getConfigLogPage(HttpServletRequest request, Page page, ConfigLog configLog) {
		Map<String,Object> params = WebUtils.getParametersStartingWith(request,StringUtils.EMPTY);
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		if (StringUtils.isNotBlank(start)) {
			page.setCurrent(Long.parseLong(start)/25 + 1);
		} else {
			page.setCurrent(1l);
		}
		if (StringUtils.isNotBlank(limit)) {
			page.setSize(Long.parseLong(limit));
		} else {
			page.setSize(20l);
		}
		List<String> keys = new ArrayList<String>();
		for(String key:params.keySet()) {
			String value = (String) params.get(key);
			if( StringUtils.isBlank(value) ) {
				keys.add(key);
			}
		}
		for(String key:keys) {
			params.remove(key);
		}
		return configLogService.queryPage(page, params);
	}

	/**
	 * 通过id查询配置操作日志表
	 * 
	 * @param logId id
	 * @return R
	 */
	@GetMapping("/{logId}")
	public R<?> getById(@PathVariable("logId") Long logId) {
		return new R(configLogService.getById(logId));
	}

}
